using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Diagnostics;

namespace TextSupport
{
	public class OutlineNode
	{
		private OutlineNodeTextSurface _textSurface;
		internal OutlineNodeTextSurface TextSurface
		{
			get { return _textSurface; }
			set { _textSurface = value; }
		}
	

		private TextFrame _frame;
		public TextFrame Frame
		{
			get { return _frame; }
			set { _frame = value; }
		}

		TextFrameView _textView;
		TextFrameController _textController;

		private bool _isExpanded;
		public bool IsExpanded
		{
			get { return _isExpanded; }
			set { _isExpanded = value; }
		}

		private XmlNode _node;
		public XmlNode Node
		{
			get { return _node; }
			set { _node = value; }
		}

		private int _depth;
		public int Depth
		{
			get { return _depth; }
		}
	
		public string DisplayText
		{
			get 
			{
				if (Node.Attributes["text"] == null)
					return "";

				return Node.Attributes["text"].Value;
			}
		}

		private float _top;
		public float Top
		{
			get { return _top; }
			set 
			{ 
				_top = value;

				RectangleF e = _frame.Extent;
				e.Y = _top;
				_frame.Extent = e;
			}
		}

		private float _height;
		public float Height
		{
			get { return _height; }
			set 
			{
				_height = value;

				RectangleF e = _frame.Extent;
				e.Height = _height;
				_frame.Extent = e;
			}
		}

		internal OutlineNode(IEditSurface surface, XmlNode node, int depth)
		{
			_node = node;
			_depth = depth;

			_frame = new TextFrame();

			_textSurface = new OutlineNodeTextSurface(surface, this);

			_textView = new TextFrameView(_frame, _textSurface);
			_textController = new TextFrameController(_frame, _textView);

			_textController.SetText(DisplayText);
		}

		/// <summary>
		/// Update Height with the height of this node based on
		/// it's contents and the surface it's going into.
		/// </summary>
		internal void Measure(IEditSurface surface, Graphics g)
		{
			RectangleF e = _frame.Extent;
			e.X = 15*_depth;
			e.Width = surface.GetExtent().Width;
			_frame.Extent = e;

			SizeF textExtent = _frame.MeasureExtent(surface, g);
			_height = textExtent.Height;
			e.Height = _height;
			_frame.Extent = e;
		}

		internal void Paint(IEditSurface surface, Graphics g)
		{
			_textView.Paint(g);
		}
	}
}
